package com.help10000.rms.ui.utils

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.util.Log
import android.view.View
import android.view.WindowInsetsController
import androidx.annotation.ColorInt


object StatusBarUtil {

    /**
     * 设置状态栏颜色（通过颜色字符串，如 "#FF0000"）
     */
    fun setColor(activity: Activity, colorHex: String?) {
        Log.d("StatusBarUtil", "setColor colorHex: $colorHex")
        if(colorHex.isNullOrEmpty()){
            restoreDefaultColor(activity)
            return
        }

        Log.d("StatusBarUtil", "setColor SDK_INT: ${Build.VERSION.SDK_INT}")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                val color = Color.parseColor(colorHex)
                activity.window.statusBarColor = color
                val isLight = isLightColor(color)
                setIconColor(isLight,activity)
            } catch (e: Exception) {
                Log.e("StatusBarUtil", "Invalid color string: $colorHex", e)
            }
        }
    }

    private fun setIconColor(isLight: Boolean, activity: Activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            // Android 11+ 使用 WindowInsetsController
            val insetsController: WindowInsetsController? = activity.window.insetsController
            insetsController?.setSystemBarsAppearance(
                if (isLight) WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS else 0,
                WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
            )
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Android 6.0 ~ 10 使用旧方式
            var visibility: Int = activity.window.decorView.getSystemUiVisibility()
            visibility = if (isLight) {
                visibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            } else {
                visibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
            }
            activity.window.decorView.setSystemUiVisibility(visibility)
        }
    }


    /**
     * 恢复为主题默认的状态栏颜色
     */
    private fun restoreDefaultColor(activity: Activity) {
        Log.d("StatusBarUtil", "restoreDefaultColor SDK_INT: ${Build.VERSION.SDK_INT}")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            var defaultColor = activity.obtainStyledAttributes(intArrayOf(android.R.attr.statusBarColor))
                .getColor(0, Color.BLACK)
            //activity.window.statusBarColor = defaultColor

            //defaultColor = Color.argb(1,255,255,255)
            defaultColor = Color.TRANSPARENT
            activity.window.statusBarColor = defaultColor

            val isLight = isLightColor(defaultColor)
            Log.d("StatusBarUtil", "restoreDefaultColor isLight: $isLight")
            setIconColor(isLight,activity)
        }
    }

    /**
     * 判断颜色是否为浅色
     */
    private fun isLightColor(@ColorInt color: Int): Boolean {
        if(Color.alpha(color).toInt() <25 ){
            return true
        }
        val r = Color.red(color).toDouble()
        val g = Color.green(color).toDouble()
        val b = Color.blue(color).toDouble()

        // 计算亮度（人眼对绿色更敏感）
        val luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255
        Log.d("StatusBarUtil", "isLightColor luminance: $luminance")
        return luminance >= 0.5
    }
}
